package com.etrans.baf.analyse.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSON;

public class DateUtils
{
	
	private static String format_yyyy_MM_dd_HH_mm_ss_SSS = "yyyy-MM-dd HH:mm:ss.SSS";
	
	private static ThreadLocal<SimpleDateFormatGetter> threadLocal = new ThreadLocal<SimpleDateFormatGetter>()
	{
		protected SimpleDateFormatGetter initialValue()
		{
			return new SimpleDateFormatGetter();
		};
	};

	public static String format(Date date)
	{
		return threadLocal.get().getByFormat(format_yyyy_MM_dd_HH_mm_ss_SSS).format(date);
	}
	
	public static String format(Date date, String format)
	{
		return threadLocal.get().getByFormat(format).format(date);
	}
	
	public static Date parse(String value, String format) throws ParseException
	{
		return threadLocal.get().getByFormat(format).parse(value);
	}

	public static Date parse(String value) throws ParseException
	{
		return threadLocal.get().getByFormat(format_yyyy_MM_dd_HH_mm_ss_SSS).parse(value);
	}
	
	/**
	 * 
	 */
	public static class SimpleDateFormatGetter
	{
		private Map<String, SimpleDateFormat> map = new HashMap<>();
		
		public SimpleDateFormat getByFormat(String _format)
		{
			if( _format==null )
			{
				return null;
			}
			SimpleDateFormat sdf = map.get(_format);
			if( sdf==null )
			{
				sdf = new SimpleDateFormat(_format);
				map.put(_format, sdf);
			}
			return sdf;
		}
	}
	/**
	 * 获取当前的分析日期(分析前一天，用-隔开日期)
	 */
	public static String getDateStrSplit() {
		Calendar now = Calendar.getInstance(); 
		int y=now.get(Calendar.YEAR);
		int m=(now.get(Calendar.MONTH) + 1);
		int d=now.get(Calendar.DAY_OF_MONTH);
		String date_str_split="";
		if(m<10){
			if(d<10){
				date_str_split=y+"-0"+m+"-0"+d;
			}else{
				date_str_split=y+"-0"+m+"-"+d;
			}
		}else{
			if(d<10){
				date_str_split=y+"-"+m+"-0"+d;
			}else{
				date_str_split=y+"-"+m+"-"+d;
			}
		}
		return date_str_split;
	}
	
	
	/**
	 * 获取当前的分析日期(分析前一天，不用-隔开日期)
	 */
	public static String getDateStrNoSplit() {
		Calendar now = Calendar.getInstance(); 
		int y=now.get(Calendar.YEAR);
		int m=(now.get(Calendar.MONTH) + 1);
		int d=now.get(Calendar.DAY_OF_MONTH);
		String date_str="";
		if(m<10){
			if(d<10){
				date_str=y+"-0"+m+"-0"+d;
			}else{
				date_str=y+"-0"+m+"-"+d;
			}
		}else{
			if(d<10){
				date_str=y+"-"+m+"-0"+d;
			}else{
				date_str=y+"-"+m+"-"+d;
			}
		}
		return date_str;
	}
	/**
	 * D_M 
	 *   0取前一天 (analyse_date不为空取analyse_date 为空取前一天)
	 *   1 获取前一个月                -1 (analyse_date-1)
	 * @param analyse_date
	 * @param D_M
	 * @return
	 */
	public static String getAnalyseDate(String analyse_date,int D_M){
		Calendar cl = Calendar.getInstance();
		String date = null;
		try {
			if(analyse_date != null && analyse_date.trim().length()>0){
			    cl.setTime(parse(analyse_date, Constants.DATE_YYYY_MM_DD));
				if(D_M==0){
					date= analyse_date;
				}else if(D_M==1){
					cl.add(Calendar.MONTH, -1);	
					date=format(cl.getTime(),Constants.DATE_YYYY_MM_DD);
				}else if(D_M==-1){
					cl.add(Calendar.DAY_OF_YEAR, -1);
					date=format(cl.getTime(),Constants.DATE_YYYY_MM_DD);
				}
			}else{ 
				if(D_M==0)
					cl.add(Calendar.DAY_OF_YEAR, -1);
				else if(D_M==1)
					cl.add(Calendar.MONTH, -1);	
				date=format(cl.getTime(),Constants.DATE_YYYY_MM_DD);
			} 
		} catch (Exception e) {
			if(D_M==0)
			cl.add(Calendar.DAY_OF_YEAR, -1);
			else if(D_M==1)
			cl.add(Calendar.MONTH, -1);	
			date=format(cl.getTime(),Constants.DATE_YYYY_MM_DD);
		}
		return  date;
	}
	/**
	 * 获取前一小时
	 * @param analyse_date DATE_YYYY_MM_DD_HH
	 * @return
	 */
	public static String getBeforeOne(String analyse_date){
		Calendar cl = Calendar.getInstance();
		String date = null;
		try {
			if(analyse_date != null && analyse_date.trim().length()>0){
			    cl.setTime(parse(analyse_date, Constants.DATE_YYYY_MM_DD_HH));
				date= analyse_date;
			}else{
				cl.add(Calendar.HOUR_OF_DAY, -1);	
				date=format(cl.getTime(),Constants.DATE_YYYY_MM_DD_HH);
			} 
		} catch (Exception e) {
			cl.add(Calendar.HOUR_OF_DAY, -1);	
			date=format(cl.getTime(),Constants.DATE_YYYY_MM_DD_HH);
		}
		return  date;
	}
	/**
	 * 获取上周一
	 * @param analyse_date 指定日期直接返回当前日期
	 * @return
	 */
	public static String getLastWeekOne(String analyse_date){
		Calendar cl = Calendar.getInstance();
		String date = null;
		try {
			if(analyse_date != null && analyse_date.trim().length()>0){
			    cl.setTime(parse(analyse_date, Constants.DATE_YYYY_MM_DD));
				date= analyse_date;
			}else{
				cl.set(Calendar.DAY_OF_WEEK,1);
				cl.add(Calendar.DATE, -6);	
				date=format(cl.getTime(),Constants.DATE_YYYY_MM_DD);
			} 
		} catch (Exception e) {
			cl.set(Calendar.DAY_OF_WEEK,1);
			cl.add(Calendar.DATE, -6);	
			date=format(cl.getTime(),Constants.DATE_YYYY_MM_DD);
		}
		return  date;
	}
	/**
	 * 上周日
	 * @param analyse_date 
	 * @return
	 */
	public static String getLastWeekSeven(String analyse_date){
		Calendar cl = Calendar.getInstance();
		String date = null;
		try {
			if(analyse_date != null && analyse_date.trim().length()>0){
			    cl.setTime(parse(analyse_date, Constants.DATE_YYYY_MM_DD));
				cl.add(Calendar.DATE, 6);	
				date=format(cl.getTime(),Constants.DATE_YYYY_MM_DD);
			}else{
				cl.set(Calendar.DAY_OF_WEEK,1);
				cl.add(Calendar.DATE, 0);	
				date=format(cl.getTime(),Constants.DATE_YYYY_MM_DD);
			} 
		} catch (Exception e) {
			cl.set(Calendar.DAY_OF_WEEK,1);
			cl.add(Calendar.DATE, 0);	
			date=format(cl.getTime(),Constants.DATE_YYYY_MM_DD);
		}
		return  date;
	}
	/**
	 * 取上个月第一天
	 */
	public static String getLastMonthOne(){
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MONTH, -1);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		String date=format(calendar.getTime(),Constants.DATE_YYYY_MM_DD);
		return date;
	}
	public static String nextMonthFirstDate() { 
	        Calendar calendar = Calendar.getInstance(); 
	        calendar.set(Calendar.DAY_OF_MONTH, 1); 
	        calendar.add(Calendar.MONTH, 1); 
	        String date=format(calendar.getTime(),Constants.DATE_YYYY_MM_DD);
			return date;
	 }
	/**
	 * 取上个月最后一天
	 * @return
	 */
	public static String getLastMonthLastDay(){
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_MONTH, 1); //设置本月第一天
		calendar.add(Calendar.DATE, -1);
		String date=format(calendar.getTime(),Constants.DATE_YYYY_MM_DD);
		return date;
	}
	/**
	 * 取得上一个季度第一天
	 * @return
	 */
	public static String getLastSeasonTimeStr(){
		//1-3 第一季度  4-6 二季度 7-9 三季度  10-12 四季度
		Calendar c = Calendar.getInstance();  
        int month =c.get(Calendar.MONTH); 
        int year = c.get(Calendar.YEAR);
        if(month<=2){//取去年的时间
    		c.set(Calendar.MONTH, 9);
    		c.set(Calendar.DAY_OF_MONTH, 1);
        	c.set(Calendar.YEAR, year-1);
    		String sdate=format(c.getTime(),Constants.DATE_YYYY_MM_DD);
    		c.set(Calendar.MONTH, 11);
    		c.set(Calendar.DATE, 31); 
    		String edate=format(c.getTime(),Constants.DATE_YYYY_MM_DD);
     		return sdate+"="+edate;
        }else if(month>2 && month<=5){//取第一季度
        	c.set(Calendar.MONTH, 0);
    		c.set(Calendar.DAY_OF_MONTH, 1); //设置本月第一天
    		String sdate=format(c.getTime(),Constants.DATE_YYYY_MM_DD);
    		c.set(Calendar.MONTH, 3);
    		c.set(Calendar.DATE, -1); 
    		String edate=format(c.getTime(),Constants.DATE_YYYY_MM_DD);
    		return sdate+"="+edate;
        }else if(month>5 && month<=8){//第二
        	c.set(Calendar.MONTH, 3);
    		c.set(Calendar.DAY_OF_MONTH, 1); //设置本月第一天
    		String sdate=format(c.getTime(),Constants.DATE_YYYY_MM_DD);
    		c.set(Calendar.MONTH, 5);
    		c.set(Calendar.DAY_OF_MONTH,30); 
    		String edate=format(c.getTime(),Constants.DATE_YYYY_MM_DD);
    		return sdate+"="+edate;
         }else{//取第三季度
        	c.set(Calendar.MONTH, 6);
     		c.set(Calendar.DAY_OF_MONTH, 1); //设置本月第一天
     		String sdate=format(c.getTime(),Constants.DATE_YYYY_MM_DD);
     		c.set(Calendar.MONTH, 8);
     		c.set(Calendar.DAY_OF_MONTH, 30); 
     		String edate=format(c.getTime(),Constants.DATE_YYYY_MM_DD);
     		return sdate+"="+edate;
        }
 	}
	/**
	 * 取当月最后一天
	 * @return
	 */
	public static String getCurMonthLastDay(){
		Calendar c = Calendar.getInstance();  
		c.add(Calendar.MONTH, 1);  
	    c.set(Calendar.DAY_OF_MONTH, 0); 
 		String edate=format(c.getTime(),Constants.DATE_YYYY_MM_DD);
 		return edate;
	}
	public static void main(String[] args) throws ParseException
	{
		String current_mon_day = DateUtils.format(new Date(),Constants.DATE_YYYY_MM_DD);
		String day = current_mon_day.substring(current_mon_day.length()-2,current_mon_day.length());
	
		System.out.println(getPreviousDayDate());
		 
	}
	
	public static long getTimeLong(String _time) {
		long timeLong = 1498665600000L;
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
			timeLong = sdf.parse(_time).getTime();
		} catch (Exception e) {
		}
		return timeLong;
	}
	
	public static long getTimeLong(Date _date) {
		long timeLong = 1498665600000L;
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
			String time = sdf.format(_date);
			timeLong = sdf.parse(time).getTime();
		} catch (Exception e) {
		}
		return timeLong;		
	}
	
	/**
	 * <p>
	 * 	Date转时间型String
	 * </p>
	 * 
	 * @param _value Date
	 * @param formate String
	 * @return _value String
	 */
	public static String getDateTimeString(Date _value,String formate) {
		if(null==formate)formate="yyyy-MM-dd HH:mm:ss";
		SimpleDateFormat sdf = new SimpleDateFormat(formate);
		return sdf.format(_value);
	}
	 /**
     * 计算两个日期之间相差的秒数
     * @param startdate 较小的时间 
     * @param enddate 较大的时间 
     * @return 相差秒数 
     * @throws ParseException
     */
    public static int SecondsBetween(Date starttime,Date endtime)  {
		try {
			
			Calendar cal = Calendar.getInstance();
			cal.setTime(starttime);
			long time1 = cal.getTimeInMillis();
			cal.setTime(endtime);
			long time2 = cal.getTimeInMillis();
			long between_days = (time2 - time1) / 1000;
			return Integer.parseInt(String.valueOf(between_days));
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
	} 
    
    /**
	 * <title>获取前一天的日期</title>
	 * <code>getPreviousDayDate("yyyy-MM-dd")</code>
	 *
	 * @return Date date
	 */
	public static Date getPreviousDayDate(){
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DATE, -1); //得到前一天
		Date date = calendar.getTime();
		return date;
	}
    
	/**
	 * 获取某一天0点
	 * @param date 某一天日期
	 * @return 
	 */
	public static String getDayStart(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(date) + " 00:00:00";
	}
	/**
	 * 获取某一天23点59分59秒
	 * @param date 某一天日期
	 * @return 
	 */
	public static String getDayEnd(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(date) + " 23:59:59";
	}
}
